package com.mediacloud.app.newsmodule.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.mediacloud.app.assembly.views.ImageButtonX;
import com.mediacloud.app.assembly.views.NetImageViewE;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.utils.ComponentClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import org.json.JSONObject;

import java.util.List;

@SuppressLint("HandlerLeak")
public class HorseNightTwoView extends RelativeLayout implements View.OnClickListener {

    protected int mContainerMesureWidh = 0;
    protected int mContainerMesurHeight = 0;
    protected int mContainerLookWidth = 0;
    protected ImageButtonX mTop_back;
    protected NetImageViewE ass_top_titel_image;
    protected TextView ass_top_titel2;
    private int moveSum = 0;
    protected int topMargin = 15;
    public int moveSpeed = 0;
    protected long uptimeMillis = 5000;
    protected List<ArticleItem> hotDataItems;
    protected String TAG = getClass().getName();
    public boolean hadAdded = false;
    int title_type;
    RelativeLayout mScrollerContainer;
    MoreListener moreListener;
    ViewFlipper viewFlipper;

    public HorseNightTwoView(Context context) {
        this(context, null);
    }

    public HorseNightTwoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View horseNight = LayoutInflater.from(context).inflate(
                R.layout.home_horse_night_two_news, null);
        addView(horseNight);
        viewFlipper = horseNight.findViewById(R.id.viewFlipper);
        mScrollerContainer = horseNight
                .findViewById(R.id.mScrollerContainer);
        ass_top_titel2 = horseNight
                .findViewById(R.id.ass_top_titel2);
        mTop_back = horseNight.findViewById(R.id.mTop_back);
        ass_top_titel_image = horseNight
                .findViewById(R.id.ass_top_titel_image);
        final int mainColor = AppFactoryGlobalConfig.getAppServerConfigInfo(context).getMainColor();
        mTop_back.setTint(mainColor, mainColor);
        /*horsecListener1 = new HorsecListener1();*/
        moreListener = new MoreListener();
        ass_top_titel2.setOnClickListener(moreListener);
        ass_top_titel_image.setOnClickListener(moreListener);
        mTop_back.setOnClickListener(moreListener);
    }


    public void removeAllData() {
        for (int i = 0; i < viewFlipper.getChildCount(); i++) {
            View child = viewFlipper.getChildAt(i);
            if (child instanceof ViewGroup) {
                ViewGroup chg = ((ViewGroup) child);
                for (int j = 0; j < chg.getChildCount(); j++) {
                    chg.getChildAt(j).setVisibility(View.INVISIBLE);
                }
            }
        }
        viewFlipper.removeAllViews();
        viewFlipper.stopFlipping();
    }

    private ComponentItem componentItem;

    public void addHorsenightItem(final List<ArticleItem> data,
                                  final ComponentItem componentItem, int title_type) {
        moveSum = 0;
        this.componentItem = componentItem;
        for (int i = 0; i < data.size(); i += 2) {
            ArticleItem item = data.get(i);

            ArticleItem item1 = null;
            if (i + 1 >= data.size()) {
                item1 = null;
            } else {
                item1 = data.get(i + 1);
            }
            String newsType = null;
            String newsTitle = item != null ? item.getTitle() : "";
            String newsTitle1 = item1 != null ? item1.getTitle() : "";
			/*String typeStartMark = getResources().getString(
					R.string.home_newstype_startmark);
			if (!TextUtils.isEmpty(newsTitle)&&newsTitle.startsWith(typeStartMark)) {
				String typeEndMark = getResources().getString(
						R.string.home_newstype_endmark);
				newsType = newsTitle.substring(0,
						newsTitle.indexOf(typeEndMark));
				newsType = newsType.replace(typeStartMark, "").replace(
						typeEndMark, "");
				newsTitle = newsTitle
						.substring(newsTitle.indexOf(typeEndMark) + 1);
			} else {
				newsType = getResources().getString(R.string.news_report);
			}
			if (!TextUtils.isEmpty(newsTitle1)&&newsTitle1.startsWith(typeStartMark)) {
				String typeEndMark = getResources().getString(
						R.string.home_newstype_endmark);
				newsType = newsTitle1.substring(0,
						newsTitle1.indexOf(typeEndMark));
				newsType = newsType.replace(typeStartMark, "").replace(
						typeEndMark, "");
				newsTitle1 = newsTitle1.substring(newsTitle1
						.indexOf(typeEndMark) + 1);
			} else {
				newsType = getResources().getString(R.string.news_report);
			}
*/
            View sbView = LayoutInflater.from(getContext()).inflate(R.layout.gundongbobao2, null, true);
            TextView gundong1 = sbView.findViewById(R.id.gundong1);
            gundong1.setText(newsTitle);
            TextView gundong2 = sbView.findViewById(R.id.gundong2);

            viewFlipper.addView(sbView);
            gundong2.setText(newsTitle1);
            gundong2.setTag(item1);
            gundong2.setOnClickListener(this);
            gundong1.setOnClickListener(this);
            gundong1.setTag(item);
        }
        viewFlipper.startFlipping();
    }

    public void refreshData(List<ArticleItem> data,
                            ComponentItem componentItem, int title_type) {
        moreListener.componentItem = componentItem;
        if (title_type == 1) {
            ass_top_titel2.setVisibility(View.VISIBLE);
            ass_top_titel2.setText(componentItem.getTitle());
            JSONObject other_field = componentItem.orginDataObject.optJSONObject("other_field");
            if (other_field != null) {
                int title_color_type = other_field.optInt("title_color_type");
                if (title_color_type == 1){
                    String title_color = other_field.optString("title_color", "#FF000000");
                    try {
                        ass_top_titel2.setTextColor(Color.parseColor(title_color));
                    } catch (Exception e) {
                        e.printStackTrace();
                        ass_top_titel2.setTextColor(DefaultBgUtil.getTintColor(getContext()));
                    }
                }else{
                    ass_top_titel2.setTextColor(DefaultBgUtil.getTintColor(getContext()));
                }
            } else {

                ass_top_titel2.setTextColor(DefaultBgUtil.getTintColor(getContext()));
            }

        } else if (title_type == 2) {
            ass_top_titel_image.setVisibility(View.VISIBLE);
            ass_top_titel_image.load(componentItem.getTitle());
        }

        if (data != null && data.size() > 0) {
            hotDataItems = data;
            try {
                hadAdded = true;
            } catch (Exception e) {
            }
            removeAllData();
            addHorsenightItem(hotDataItems, componentItem, title_type);
        } else if (hotDataItems != null && hotDataItems.size() > 0) {
            try {
                hadAdded = true;
            } catch (Exception e) {
            }
            removeAllData();
            addHorsenightItem(hotDataItems, componentItem, title_type);
        } else {
            try {
                hadAdded = false;
            } catch (Exception e) {
            }
        }


        class HorsecListener implements OnClickListener {
            ComponentItem componentItem;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int index = (int) v.getTag();
                JSONObject data = hotDataItems.get(index).orginDataObject;
                ComponentClickUtils.OpenItemComponent(getContext(), componentItem,
                        data);
            }

        }

        class HorsecListener1 implements OnClickListener {
            ComponentItem componentItem;
            JSONObject data;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (componentItem != null) {
                    int index = (int) v.getTag();
                    data = hotDataItems.get(index).orginDataObject;
                    ComponentClickUtils.OpenItemComponent(getContext(),
                            componentItem, data);
                }
            }

        }

    }

    @Override
    public void onClick(View v) {
        if (v.getTag() != null) {
            JSONObject data = (JSONObject) ((ArticleItem) v.getTag()).extendField;
            ComponentClickUtils.OpenItemComponent(v.getContext(), componentItem, data);
        }
    }

    public class MoreListener implements View.OnClickListener {
        ComponentItem componentItem;

        @Override
        public void onClick(View v) {
            if (componentItem != null) {
                ComponentClickUtils.OpenItemComponent(getContext(),
                        componentItem, null);
            }

        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (viewFlipper != null)
            viewFlipper.stopFlipping();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (viewFlipper != null && viewFlipper.getChildCount() > 0)
            viewFlipper.startFlipping();
    }
}